using System; 
using System.Text; 
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration; 
using System.Xml; 
using System.Xml.Serialization;
using SubSonic; 
using SubSonic.Utilities;

namespace Adex
{
	/// <summary>
	/// Strongly-typed collection for the Company class.
	/// </summary>
	[Serializable]
	public partial class CompanyCollection : ActiveList<Company, CompanyCollection> 
	{	   
		public CompanyCollection() {}

	}

	/// <summary>
	/// This is an ActiveRecord class which wraps the Company table.
	/// </summary>
	[Serializable]
	public partial class Company : ActiveRecord<Company>
	{
		#region .ctors and Default Settings
		
		public Company()
		{
		  SetSQLProps();
		  InitSetDefaults();
		  MarkNew();
		}

		
		private void InitSetDefaults() { SetDefaults(); }

		
		public Company(bool useDatabaseDefaults)
		{
			SetSQLProps();
			if(useDatabaseDefaults)
				ForceDefaults();
			MarkNew();
		}

		public Company(object keyID)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByKey(keyID);
		}

		 
		public Company(string columnName, object columnValue)
		{
			SetSQLProps();
			InitSetDefaults();
			LoadByParam(columnName,columnValue);
		}

		
		protected static void SetSQLProps() { GetTableSchema(); }

		
		#endregion
		
		#region Schema and Query Accessor
		public static Query CreateQuery() { return new Query(Schema); }

		
		public static TableSchema.Table Schema
		{
			get
			{
				if (BaseSchema == null)
					SetSQLProps();
				return BaseSchema;
			}

		}

		
		private static void GetTableSchema() 
		{
			if(!IsSchemaInitialized)
			{
				//Schema declaration
				TableSchema.Table schema = new TableSchema.Table("Company", TableType.Table, DataService.GetInstance("Adex"));
				schema.Columns = new TableSchema.TableColumnCollection();
				schema.SchemaName = @"dbo";
				//columns
				
				TableSchema.TableColumn colvarCompanyCode = new TableSchema.TableColumn(schema);
				colvarCompanyCode.ColumnName = "CompanyCode";
				colvarCompanyCode.DataType = DbType.Int32;
				colvarCompanyCode.MaxLength = 0;
				colvarCompanyCode.AutoIncrement = true;
				colvarCompanyCode.IsNullable = false;
				colvarCompanyCode.IsPrimaryKey = true;
				colvarCompanyCode.IsForeignKey = false;
				colvarCompanyCode.IsReadOnly = false;
				colvarCompanyCode.DefaultSetting = @"";
				colvarCompanyCode.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCompanyCode);
				
				TableSchema.TableColumn colvarCompanyName = new TableSchema.TableColumn(schema);
				colvarCompanyName.ColumnName = "CompanyName";
				colvarCompanyName.DataType = DbType.String;
				colvarCompanyName.MaxLength = 255;
				colvarCompanyName.AutoIncrement = false;
				colvarCompanyName.IsNullable = true;
				colvarCompanyName.IsPrimaryKey = false;
				colvarCompanyName.IsForeignKey = false;
				colvarCompanyName.IsReadOnly = false;
				colvarCompanyName.DefaultSetting = @"";
				colvarCompanyName.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCompanyName);
				
				TableSchema.TableColumn colvarLogo = new TableSchema.TableColumn(schema);
				colvarLogo.ColumnName = "Logo";
				colvarLogo.DataType = DbType.String;
				colvarLogo.MaxLength = 50;
				colvarLogo.AutoIncrement = false;
				colvarLogo.IsNullable = true;
				colvarLogo.IsPrimaryKey = false;
				colvarLogo.IsForeignKey = false;
				colvarLogo.IsReadOnly = false;
				colvarLogo.DefaultSetting = @"";
				colvarLogo.ForeignKeyTableName = "";
				schema.Columns.Add(colvarLogo);
				
				TableSchema.TableColumn colvarValidFrom = new TableSchema.TableColumn(schema);
				colvarValidFrom.ColumnName = "ValidFrom";
				colvarValidFrom.DataType = DbType.DateTime;
				colvarValidFrom.MaxLength = 0;
				colvarValidFrom.AutoIncrement = false;
				colvarValidFrom.IsNullable = true;
				colvarValidFrom.IsPrimaryKey = false;
				colvarValidFrom.IsForeignKey = false;
				colvarValidFrom.IsReadOnly = false;
				colvarValidFrom.DefaultSetting = @"";
				colvarValidFrom.ForeignKeyTableName = "";
				schema.Columns.Add(colvarValidFrom);
				
				TableSchema.TableColumn colvarValidTo = new TableSchema.TableColumn(schema);
				colvarValidTo.ColumnName = "ValidTo";
				colvarValidTo.DataType = DbType.DateTime;
				colvarValidTo.MaxLength = 0;
				colvarValidTo.AutoIncrement = false;
				colvarValidTo.IsNullable = true;
				colvarValidTo.IsPrimaryKey = false;
				colvarValidTo.IsForeignKey = false;
				colvarValidTo.IsReadOnly = false;
				colvarValidTo.DefaultSetting = @"";
				colvarValidTo.ForeignKeyTableName = "";
				schema.Columns.Add(colvarValidTo);
				
				TableSchema.TableColumn colvarCredits = new TableSchema.TableColumn(schema);
				colvarCredits.ColumnName = "Credits";
				colvarCredits.DataType = DbType.Int32;
				colvarCredits.MaxLength = 0;
				colvarCredits.AutoIncrement = false;
				colvarCredits.IsNullable = true;
				colvarCredits.IsPrimaryKey = false;
				colvarCredits.IsForeignKey = false;
				colvarCredits.IsReadOnly = false;
				
						colvarCredits.DefaultSetting = @"((0))";
				colvarCredits.ForeignKeyTableName = "";
				schema.Columns.Add(colvarCredits);
				
				TableSchema.TableColumn colvarUnlimitedCredits = new TableSchema.TableColumn(schema);
				colvarUnlimitedCredits.ColumnName = "UnlimitedCredits";
				colvarUnlimitedCredits.DataType = DbType.Boolean;
				colvarUnlimitedCredits.MaxLength = 0;
				colvarUnlimitedCredits.AutoIncrement = false;
				colvarUnlimitedCredits.IsNullable = false;
				colvarUnlimitedCredits.IsPrimaryKey = false;
				colvarUnlimitedCredits.IsForeignKey = false;
				colvarUnlimitedCredits.IsReadOnly = false;
				
						colvarUnlimitedCredits.DefaultSetting = @"((0))";
				colvarUnlimitedCredits.ForeignKeyTableName = "";
				schema.Columns.Add(colvarUnlimitedCredits);
				
				TableSchema.TableColumn colvarUnlimitedCategories = new TableSchema.TableColumn(schema);
				colvarUnlimitedCategories.ColumnName = "UnlimitedCategories";
				colvarUnlimitedCategories.DataType = DbType.Boolean;
				colvarUnlimitedCategories.MaxLength = 0;
				colvarUnlimitedCategories.AutoIncrement = false;
				colvarUnlimitedCategories.IsNullable = false;
				colvarUnlimitedCategories.IsPrimaryKey = false;
				colvarUnlimitedCategories.IsForeignKey = false;
				colvarUnlimitedCategories.IsReadOnly = false;
				
						colvarUnlimitedCategories.DefaultSetting = @"((0))";
				colvarUnlimitedCategories.ForeignKeyTableName = "";
				schema.Columns.Add(colvarUnlimitedCategories);
				
				TableSchema.TableColumn colvarPricePerSecond = new TableSchema.TableColumn(schema);
				colvarPricePerSecond.ColumnName = "PricePerSecond";
				colvarPricePerSecond.DataType = DbType.Double;
				colvarPricePerSecond.MaxLength = 0;
				colvarPricePerSecond.AutoIncrement = false;
				colvarPricePerSecond.IsNullable = true;
				colvarPricePerSecond.IsPrimaryKey = false;
				colvarPricePerSecond.IsForeignKey = false;
				colvarPricePerSecond.IsReadOnly = false;
				
						colvarPricePerSecond.DefaultSetting = @"((0))";
				colvarPricePerSecond.ForeignKeyTableName = "";
				schema.Columns.Add(colvarPricePerSecond);
				
				TableSchema.TableColumn colvarNewsTemplateID = new TableSchema.TableColumn(schema);
				colvarNewsTemplateID.ColumnName = "NewsTemplateID";
				colvarNewsTemplateID.DataType = DbType.Int32;
				colvarNewsTemplateID.MaxLength = 0;
				colvarNewsTemplateID.AutoIncrement = false;
				colvarNewsTemplateID.IsNullable = true;
				colvarNewsTemplateID.IsPrimaryKey = false;
				colvarNewsTemplateID.IsForeignKey = true;
				colvarNewsTemplateID.IsReadOnly = false;
				colvarNewsTemplateID.DefaultSetting = @"";
				
					colvarNewsTemplateID.ForeignKeyTableName = "NewsTemplate";
				schema.Columns.Add(colvarNewsTemplateID);
				
				TableSchema.TableColumn colvarActive = new TableSchema.TableColumn(schema);
				colvarActive.ColumnName = "Active";
				colvarActive.DataType = DbType.Boolean;
				colvarActive.MaxLength = 0;
				colvarActive.AutoIncrement = false;
				colvarActive.IsNullable = false;
				colvarActive.IsPrimaryKey = false;
				colvarActive.IsForeignKey = false;
				colvarActive.IsReadOnly = false;
				
						colvarActive.DefaultSetting = @"((0))";
				colvarActive.ForeignKeyTableName = "";
				schema.Columns.Add(colvarActive);
				
				TableSchema.TableColumn colvarDateAdded = new TableSchema.TableColumn(schema);
				colvarDateAdded.ColumnName = "DateAdded";
				colvarDateAdded.DataType = DbType.DateTime;
				colvarDateAdded.MaxLength = 0;
				colvarDateAdded.AutoIncrement = false;
				colvarDateAdded.IsNullable = false;
				colvarDateAdded.IsPrimaryKey = false;
				colvarDateAdded.IsForeignKey = false;
				colvarDateAdded.IsReadOnly = false;
				
						colvarDateAdded.DefaultSetting = @"(getdate())";
				colvarDateAdded.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDateAdded);
				
				TableSchema.TableColumn colvarDateModified = new TableSchema.TableColumn(schema);
				colvarDateModified.ColumnName = "DateModified";
				colvarDateModified.DataType = DbType.DateTime;
				colvarDateModified.MaxLength = 0;
				colvarDateModified.AutoIncrement = false;
				colvarDateModified.IsNullable = false;
				colvarDateModified.IsPrimaryKey = false;
				colvarDateModified.IsForeignKey = false;
				colvarDateModified.IsReadOnly = false;
				
						colvarDateModified.DefaultSetting = @"(getdate())";
				colvarDateModified.ForeignKeyTableName = "";
				schema.Columns.Add(colvarDateModified);
				
				TableSchema.TableColumn colvarUnlimitedCommercialCategories = new TableSchema.TableColumn(schema);
				colvarUnlimitedCommercialCategories.ColumnName = "UnlimitedCommercialCategories";
				colvarUnlimitedCommercialCategories.DataType = DbType.Boolean;
				colvarUnlimitedCommercialCategories.MaxLength = 0;
				colvarUnlimitedCommercialCategories.AutoIncrement = false;
				colvarUnlimitedCommercialCategories.IsNullable = false;
				colvarUnlimitedCommercialCategories.IsPrimaryKey = false;
				colvarUnlimitedCommercialCategories.IsForeignKey = false;
				colvarUnlimitedCommercialCategories.IsReadOnly = false;
				
						colvarUnlimitedCommercialCategories.DefaultSetting = @"((0))";
				colvarUnlimitedCommercialCategories.ForeignKeyTableName = "";
				schema.Columns.Add(colvarUnlimitedCommercialCategories);
				
				BaseSchema = schema;
				//add this schema to the provider
				//so we can query it later
				DataService.Providers["Adex"].AddSchema("Company",schema);
			}

		}

		#endregion
		
		#region Props
		
		  
		[XmlAttribute("CompanyCode")]
		public int CompanyCode 
		{
			get { return GetColumnValue<int>("CompanyCode"); }

			set { SetColumnValue("CompanyCode", value); }

		}

		  
		[XmlAttribute("CompanyName")]
		public string CompanyName 
		{
			get { return GetColumnValue<string>("CompanyName"); }

			set { SetColumnValue("CompanyName", value); }

		}

		  
		[XmlAttribute("Logo")]
		public string Logo 
		{
			get { return GetColumnValue<string>("Logo"); }

			set { SetColumnValue("Logo", value); }

		}

		  
		[XmlAttribute("ValidFrom")]
		public DateTime? ValidFrom 
		{
			get { return GetColumnValue<DateTime?>("ValidFrom"); }

			set { SetColumnValue("ValidFrom", value); }

		}

		  
		[XmlAttribute("ValidTo")]
		public DateTime? ValidTo 
		{
			get { return GetColumnValue<DateTime?>("ValidTo"); }

			set { SetColumnValue("ValidTo", value); }

		}

		  
		[XmlAttribute("Credits")]
		public int? Credits 
		{
			get { return GetColumnValue<int?>("Credits"); }

			set { SetColumnValue("Credits", value); }

		}

		  
		[XmlAttribute("UnlimitedCredits")]
		public bool UnlimitedCredits 
		{
			get { return GetColumnValue<bool>("UnlimitedCredits"); }

			set { SetColumnValue("UnlimitedCredits", value); }

		}

		  
		[XmlAttribute("UnlimitedCategories")]
		public bool UnlimitedCategories 
		{
			get { return GetColumnValue<bool>("UnlimitedCategories"); }

			set { SetColumnValue("UnlimitedCategories", value); }

		}

		  
		[XmlAttribute("PricePerSecond")]
		public double? PricePerSecond 
		{
			get { return GetColumnValue<double?>("PricePerSecond"); }

			set { SetColumnValue("PricePerSecond", value); }

		}

		  
		[XmlAttribute("NewsTemplateID")]
		public int? NewsTemplateID 
		{
			get { return GetColumnValue<int?>("NewsTemplateID"); }

			set { SetColumnValue("NewsTemplateID", value); }

		}

		  
		[XmlAttribute("Active")]
		public bool Active 
		{
			get { return GetColumnValue<bool>("Active"); }

			set { SetColumnValue("Active", value); }

		}

		  
		[XmlAttribute("DateAdded")]
		public DateTime DateAdded 
		{
			get { return GetColumnValue<DateTime>("DateAdded"); }

			set { SetColumnValue("DateAdded", value); }

		}

		  
		[XmlAttribute("DateModified")]
		public DateTime DateModified 
		{
			get { return GetColumnValue<DateTime>("DateModified"); }

			set { SetColumnValue("DateModified", value); }

		}

		  
		[XmlAttribute("UnlimitedCommercialCategories")]
		public bool UnlimitedCommercialCategories 
		{
			get { return GetColumnValue<bool>("UnlimitedCommercialCategories"); }

			set { SetColumnValue("UnlimitedCommercialCategories", value); }

		}

		
		#endregion
		
		
		#region PrimaryKey Methods
		
		public Adex.CompanyCategoryCollection CompanyCategoryRecords()
		{
			return new Adex.CompanyCategoryCollection().Where(CompanyCategory.Columns.CompanyCode, CompanyCode).Load();
		}

		public Adex.CompanyModuleCollection CompanyModuleRecords()
		{
			return new Adex.CompanyModuleCollection().Where(CompanyModule.Columns.CompanyCode, CompanyCode).Load();
		}

		public Adex.CompanyNewCommercialCategoryCollection CompanyNewCommercialCategoryRecords()
		{
			return new Adex.CompanyNewCommercialCategoryCollection().Where(CompanyNewCommercialCategory.Columns.CompanyCode, CompanyCode).Load();
		}

		public Adex.OrderCollection OrderRecords()
		{
			return new Adex.OrderCollection().Where(Order.Columns.CompanyCode, CompanyCode).Load();
		}

		public Adex.SiteUserCollection SiteUserRecords()
		{
			return new Adex.SiteUserCollection().Where(SiteUser.Columns.CompanyCode, CompanyCode).Load();
		}

		#endregion
		
			
		
		#region ForeignKey Properties
		
		/// <summary>
		/// Returns a NewsTemplate ActiveRecord object related to this Company
		/// 
		/// </summary>
		public Adex.NewsTemplate NewsTemplate
		{
			get { return Adex.NewsTemplate.FetchByID(this.NewsTemplateID); }

			set { SetColumnValue("NewsTemplateID", value.TemplateID); }

		}

		
		
		#endregion
		
		
		
		//no ManyToMany tables defined (0)
		
		#region ObjectDataSource support
		
		
		/// <summary>
		/// Inserts a record, can be used with the Object Data Source
		/// </summary>
		public static void Insert(string varCompanyName,string varLogo,DateTime? varValidFrom,DateTime? varValidTo,int? varCredits,bool varUnlimitedCredits,bool varUnlimitedCategories,double? varPricePerSecond,int? varNewsTemplateID,bool varActive,DateTime varDateAdded,DateTime varDateModified,bool varUnlimitedCommercialCategories)
		{
			Company item = new Company();
			
			item.CompanyName = varCompanyName;
			
			item.Logo = varLogo;
			
			item.ValidFrom = varValidFrom;
			
			item.ValidTo = varValidTo;
			
			item.Credits = varCredits;
			
			item.UnlimitedCredits = varUnlimitedCredits;
			
			item.UnlimitedCategories = varUnlimitedCategories;
			
			item.PricePerSecond = varPricePerSecond;
			
			item.NewsTemplateID = varNewsTemplateID;
			
			item.Active = varActive;
			
			item.DateAdded = varDateAdded;
			
			item.DateModified = varDateModified;
			
			item.UnlimitedCommercialCategories = varUnlimitedCommercialCategories;
			
		
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		
		/// <summary>
		/// Updates a record, can be used with the Object Data Source
		/// </summary>
		public static void Update(int varCompanyCode,string varCompanyName,string varLogo,DateTime? varValidFrom,DateTime? varValidTo,int? varCredits,bool varUnlimitedCredits,bool varUnlimitedCategories,double? varPricePerSecond,int? varNewsTemplateID,bool varActive,DateTime varDateAdded,DateTime varDateModified,bool varUnlimitedCommercialCategories)
		{
			Company item = new Company();
			
				item.CompanyCode = varCompanyCode;
				
				item.CompanyName = varCompanyName;
				
				item.Logo = varLogo;
				
				item.ValidFrom = varValidFrom;
				
				item.ValidTo = varValidTo;
				
				item.Credits = varCredits;
				
				item.UnlimitedCredits = varUnlimitedCredits;
				
				item.UnlimitedCategories = varUnlimitedCategories;
				
				item.PricePerSecond = varPricePerSecond;
				
				item.NewsTemplateID = varNewsTemplateID;
				
				item.Active = varActive;
				
				item.DateAdded = varDateAdded;
				
				item.DateModified = varDateModified;
				
				item.UnlimitedCommercialCategories = varUnlimitedCommercialCategories;
				
			item.IsNew = false;
			if (System.Web.HttpContext.Current != null)
				item.Save(System.Web.HttpContext.Current.User.Identity.Name);
			else
				item.Save(System.Threading.Thread.CurrentPrincipal.Identity.Name);
		}

		#endregion
		#region Columns Struct
		public struct Columns
		{
			 public static string CompanyCode = @"CompanyCode";
			 public static string CompanyName = @"CompanyName";
			 public static string Logo = @"Logo";
			 public static string ValidFrom = @"ValidFrom";
			 public static string ValidTo = @"ValidTo";
			 public static string Credits = @"Credits";
			 public static string UnlimitedCredits = @"UnlimitedCredits";
			 public static string UnlimitedCategories = @"UnlimitedCategories";
			 public static string PricePerSecond = @"PricePerSecond";
			 public static string NewsTemplateID = @"NewsTemplateID";
			 public static string Active = @"Active";
			 public static string DateAdded = @"DateAdded";
			 public static string DateModified = @"DateModified";
			 public static string UnlimitedCommercialCategories = @"UnlimitedCommercialCategories";
						
		}

		#endregion
	}

}

